#include chip.inc
#include vars.inc
#include circ_buffer.inc
	code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
HIGH_PRIORITY_INTERRUPT:
	global HIGH_PRIORITY_INTERRUPT
	;DATA HAS CHANGED, CHECK IF CLOCK IS HIGH FOR START/STOP BITS
	BTFSS	SPORT,SCLK,A
	BRA		NO_SPECIAL_BITS_DETECTED
	
	;START/STOP BIT DETECTED
	BTFSC	SPORT,SDAT,A
	BRA		STOP_BIT_DETECTED
	
START_BIT_DETECTED;
	BCF		SSPCON1,SSPEN,A		;RESET SPI MODULE
	BSF		SSPCON1,SSPEN,A
	CLRF	LOOK_FOR_ACK_NACK,A	;DONT LOOK FOR ACK/NACK
	
	MOVLW	UART_TX_START_BIT
	MOVWF	BUFFER_PUSH_ACTION
	Buffer_Push_Action_Fast		;6-7 inst.
	
	BCF		INTCON,RABIF,A 		;CLEAR FLAG
	RETFIE 	FAST
	
STOP_BIT_DETECTED
	BCF		SSPCON1,SSPEN,A		;TURN OFF SPI MODULE
	CLRF	LOOK_FOR_ACK_NACK,A	;DONT LOOK FOR ACK/NACK
	
	MOVLW	UART_TX_STOP_BIT	;SELECT START/STOP
	MOVWF	BUFFER_PUSH_ACTION
	Buffer_Push_Action_Fast		;6-7 inst.
	
NO_SPECIAL_BITS_DETECTED:
	BCF		INTCON,RABIF,A ;CLEAR FLAG
	RETFIE 	FAST
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
LOW_PRIORITY_INTERRUPT: ; CONTEXT SAVING MUST BE USED SINCE WE MAY BE SUPERCEEDED BY A HIGH PRIORITY INTERRUPT
	global LOW_PRIORITY_INTERRUPT
	MOVWF	SHADDOW_WREG,A
	MOVFF	STATUS,SHADDOW_STATUS
	MOVFF	BSR,SHADDOW_BSR
	
	
	MOVFF 	SHADDOW_BSR,BSR
	MOVF	SHADDOW_WREG,W,A
	MOVFF	SHADDOW_STATUS,STATUS
	RETFIE
	

sendChar:
	MOVWF	BUFFER_PUSH_DATA,A
	;NOP
	MOVLW	UART_TX_CHAR_DATA
	MOVWF	BUFFER_PUSH_ACTION,A

	BCF INTCON,GIE,A						;DISABLE INTERRUPTS
	CALL	Buffer_Push					;AND SEND IT
	BSF INTCON,GIE,A						;RE-ENABLE INTERRUPTS
	return
	end